package me.zfz.business.controller.impl;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Objects;

import javax.servlet.http.HttpServletRequest;

import me.zfz.base.bean.SmartResponse;
import me.zfz.business.bean.UserInfo;
import me.zfz.business.bean.entity.JobInfo;
import me.zfz.business.bean.entity.TNVersion;
import me.zfz.business.bean.entity.TrainInfo;
import me.zfz.business.constant.enums.VersionType;
import me.zfz.business.controller.base.BaseController;
import me.zfz.business.service.JobInfoService;
import me.zfz.business.service.TrainInfoService;
import me.zfz.business.service.VersionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mixsmart.security.SecurityUtils;
import com.mixsmart.utils.CollectionUtils;
import com.mixsmart.utils.StringUtils;

/**
 * 首页
 *
 * @author author
 */
@Controller
@RequestMapping({"/index", "/"})
public class IndexController extends BaseController {

    @Autowired
    private VersionService versionServ;
    @Autowired
    private TrainInfoService trainInfoService;
    @Autowired
    private JobInfoService jobInfoService;

    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView index(HttpServletRequest request, String forward) throws Exception {
        ModelAndView modelView = new ModelAndView();
        TNVersion version = versionServ.getNewVersion(VersionType.PC);
        if (null == version) {
            version = new TNVersion();
            version.initVersion();
        }
        ModelMap modelMap = modelView.getModelMap();
        modelMap.put("version", version);
        modelMap.put("forward", forward);
        UserInfo userInfo = getUserInfoFromSession(request);
        String url = "/login";
        if (userInfo != null) {
            boolean isAdmin = !Objects.equals("01337e1d2666412fa3d028136ff710f7", userInfo.getRoleIds().get(0));
            url = isAdmin ? "/index" : "/front/index";
            if (!isAdmin) {
                SmartResponse<TrainInfo> trainInfoSmartResponse = trainInfoService.findAll();
                modelMap.put("trainList", trainInfoSmartResponse.getDatas());
                SmartResponse<JobInfo> jobInfoSmartResponse = jobInfoService.findAll();
                modelMap.put("jobList", jobInfoSmartResponse.getDatas());
            }
        }

        modelView.setViewName(url);
        return modelView;
    }

    /**
     * 首页
     *
     * @param request
     * @param modelView
     * @return
     * @throws Exception
     */
    @RequestMapping("/welcome")
    public ModelAndView welcome(HttpServletRequest request, ModelAndView modelView) throws Exception {
        UserInfo userInfo = getUserInfoFromSession(request);
        boolean isAdmin = !Objects.equals("01337e1d2666412fa3d028136ff710f7", userInfo.getRoleIds().get(0));
        if (!isAdmin) {
            ModelMap modelMap = modelView.getModelMap();
            SmartResponse<TrainInfo> trainInfoSmartResponse = trainInfoService.findAll();
            modelMap.put("trainList", trainInfoSmartResponse.getDatas());
            SmartResponse<JobInfo> jobInfoSmartResponse = jobInfoService.findAll();
            modelMap.put("jobList", jobInfoSmartResponse.getDatas());
        }
        modelView.setViewName(isAdmin ? "/welcome" : "/showPage/front_index");
        return modelView;
    }

}
